/**
 * Copyright (c) 2017-2024 Nop Platform. All rights reserved.
 * Author: canonical_entropy@163.com
 * Blog:   https://www.zhihu.com/people/canonical-entropy
 * Gitee:  https://gitee.com/canonical-entropy/nop-entropy
 * Github: https://github.com/entropy-cloud/nop-entropy
 */
package com.gitee.wsl.struct.table

import kotlin.test.Test
import kotlin.test.assertEquals


class TestCellRangeMerger {
    /**
     * 0110
     * 1110
     * 1111
     * 1111
     * 0011
     */
    @Test
    fun testMerge() {
        val str = "0110\n1110\n1111\n1111\n0011"

        val ranges: List<CellRange> = buildRanges(str)
        println(ranges)
        assertEquals("[B1:C1, A2:C2, A3:D4, C5:D5]", ranges.toString())
    }

    @Test
    fun testRange() {
        val str = "0\n1\n0\n1"

        val ranges: List<CellRange> = buildRanges(str)
        println(ranges)
        assertEquals("[A2, A4]", ranges.toString())
    }

    /**
     * 101
     * 111
     * 101
     * 011
     */
    @Test
    fun testMerge2() {
        val str = "101\n111\n101\n011"

        val ranges: List<CellRange> = buildRanges(str)
        println(ranges)
        assertEquals("[A1, A3, C1, A2:C2, C3, B4:C4]", ranges.toString())
    }

    /**
     * 010
     * 101
     * 010
     */
    @Test
    fun testMerge3() {
        val str = "010\n101\n010"

        val ranges: List<CellRange> = buildRanges(str)
        println(ranges)
        assertEquals("[A2, B1, C2, B3]", ranges.toString())
    }

    fun buildRanges(str: String): List<CellRange> {
        val merger = CellRangeMerger()

        val lines = str.split( '\n')
        var i = 0
        val n = lines.size
        while (i < n) {
            val line = lines[i]
            for (j in line.indices) {
                val c = line[j]
                if (c == '1') {
                    merger.addCell(i, j)
                }
            }
            i++
        }
        assertEquals(str, merger.display())
        return merger.mergedRanges
    }
}
